from rest_framework.decorators import action

from apps.HttpTestcase.models import Projects
from apps.HttpTestcase.filters.project import ProjectFilters
from apps.HttpTestcase.serializers import ProjectModeSerializer, ProjectNameSerializer, ProjectsRunSerializer, ProjectCreModeserializer
from utils.viewsets import *


class ProjectsListViewSet(ReadOnlyModelViewSet):
    queryset = Projects.objects.filter(is_delete=False)
    serializer_class = ProjectModeSerializer
    filter_class = ProjectFilters
    perms_map = {'get': '*'}


class ProjectsViewSet(DestroyModelMixin, CreateModelMixin, UpdateModelMixin, GenericViewSet):
    queryset = Projects.objects.filter(is_delete=False)
    serializer_class = ProjectCreModeserializer
    perms_map = {'get': '*', 'post': 'project_create',
                 'put': 'project_update', 'delete': 'project_delete'}

    def perform_destroy(self, instance):
        instance.is_delete = True
        instance.save()  # 逻辑删除

    @action(methods=['get'], detail=False)
    def names(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = ProjectNameSerializer(instance=queryset, many=True)
        return SuccessResponse(data={'data': serializer.data}, msg='查询成功！')

